//
//  CommodityOneViewController.m
//  BabyAndBabyDriver
//
//  Created by 范琦 on 16/8/12.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "CommodityOneViewController.h"
#import "CommodityMainCell.h"
#import "CommodityDetailViewController.h"
#import "CommodityModel.h"

@interface CommodityOneViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arr;

@end

@implementation CommodityOneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatTableView];
    [self netHandle];
}
- (void)netHandle
{
    self.arr = [NSMutableArray array];
    YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:@"Order/lists.json" paramDic:@{@"token":TOKEN, @"firstRow":@"0" ,@"listRows":@"20" ,@"status":@"1" } RegisterApiMethod:RequestMethodPost];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        
        NSArray *arr = request.responseBody[@"result"][@"lists"];
        for (NSDictionary *dic in arr) {
            CommodityModel *model = [[CommodityModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            [_arr addObject:model];
            
        }
        [_tableView reloadData];
    } failure:nil];
}

- (void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT - 64 - 40 - self.tabBarController.tabBar.frame.size.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 100;
    [_tableView registerNib:[UINib nibWithNibName:@"CommodityMainCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    _tableView.showsVerticalScrollIndicator = NO;
    [_tableView addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"];

    

}//下拉刷新
#warning 下拉刷新
- (void)headerRereshing
{
    //    [self.listArr removeAllObjects];
    //    self.allDic = [NSMutableDictionary dictionary];
    //    self.tableDic = [NSMutableDictionary dictionary];
    [self netHandle];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //2.结束刷新
        [_tableView headerEndRefreshing];
        //        [_tableView reloadData];
        //        [self.tableView headerEndRefreshing];
    });
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_arr.count != 0) {
        
        return _arr.count;
    }
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommodityMainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.title.text = @"未完成";
    cell.circleView.backgroundColor = [UIColor redColor];
    if (_arr.count != 0) {
        
        CommodityModel *model = _arr[indexPath.row];
        cell.doneTime.text = model.date;
        cell.name.text = [NSString stringWithFormat:@"商户名称:%@", model.name_merchant];
        cell.address.text = [NSString stringWithFormat:@"详细地址:%@",model.address];
        [[cell.btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",[model.tel stringValue]];
            UIWebView * callWebview = [[UIWebView alloc] init];
            [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
            [self.view addSubview:callWebview];
            
        }];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CommodityDetailViewController *detail = [[CommodityDetailViewController alloc]init];
    detail.str = @"未完成";
    CommodityModel *model = _arr[indexPath.row];
    detail.myId = model.myId;
    
    
    [self.navigationController pushViewController:detail animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
